package com.yootk.main;

import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.util.ClassUtils;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;

/**
 * ClassPathResource，读取项目中的资源
 */
public class Demo02_ClassPathResource {
    public static void main(String[] args) throws Exception {
//        test1();
//        test2();
        test3();
    }

    /**
     * 获取项目路径
     */
    public static void test1() {
        System.err.println("1、项目路径：" + System.getProperty("user.dir"));
        System.err.println("2、项目路径：" + Demo02_ClassPathResource.class.getClassLoader().getResource(""));
    }

    /**
     * 获取当前资源目录下的文件数据
     */
    public static void test2() throws Exception {
        Resource resource = new ClassPathResource("message.txt");
        System.err.println("文件路径：" + resource.getFile());
        InputStream input = resource.getInputStream();
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        int temp = 0;
        while ((temp = input.read()) != -1) {
            if (temp >= 'a' && temp <= 'z') {
                bos.write(Character.toUpperCase(temp));
            } else {
                bos.write(temp);
            }
        }
        input.close();
        bos.close();
        System.err.println(bos);
    }

    /**
     * 获取当前的ClassLoader
     */
    public static void test3() {
        System.err.println("1、ClassLoader：" + Demo02_ClassPathResource.class.getClassLoader());
        System.err.println("2、ClassLoader：" + ClassUtils.getDefaultClassLoader());
    }
}
